package com.by.test;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.by.config.SystemConfig.WatermarkPosition;

public class ImageUtil {
	
	/**
	 * 图片缩放
	 * @param srcBufferedImage  //原图片流文件
	 * @param destFile         //缩后图片
	 * @param destHeight      
	 * @param destWeight
	 */
	public  static void zoom(BufferedImage srcBufferedImage,File destFile,int destHeight,int destWeight){
		try {
		//获取源文件的宽高
		int srcImageHeight = srcBufferedImage.getHeight();
		int srcImageWidth  = srcBufferedImage.getWidth() ;
		//得到目的图片的宽高
		int imageHeight = destHeight;
		int imageWidth  = destWeight;
		if(srcImageHeight>=srcImageWidth){
			imageWidth = (int) Math.round((destHeight*1.0/srcImageHeight)*srcImageWidth);
		}else{
			imageHeight = (int) Math.round((destWeight*1.0/srcImageWidth)*srcImageHeight);
		}
		// 构造一个类型为预定义图像类型之一的 BufferedImage ， 其中  BufferedImage.TYPE_INT_RGB：  表示一个图像，它具有合成整数像素的 8 位 RGB 颜色分量。
		BufferedImage destBufferedImage = new BufferedImage(destWeight, destHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics2d = destBufferedImage.createGraphics();
		graphics2d.setBackground(Color.WHITE);
		graphics2d.clearRect(0, 0,destWeight,destHeight);
		//srcBufferedImage.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH):重新得到一个image的版本类型,其中 Image.SCALE_SMOOTH：  选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
		graphics2d.drawImage(srcBufferedImage.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH), (destWeight / 2) - (imageWidth / 2), (destHeight / 2) - (imageHeight / 2), null);
		graphics2d.dispose();//释放此图形的上下文以及它使用的所有系统资源。
		ImageIO.write(destBufferedImage, "JPEG", destFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
	} 
	/**
	 * 添加图片水印
	 * @param srcBufferedImage
	 * @param destFile
	 * @param watermarkFile
	 * @param watermarkPosition
	 * @param alpha
	 */
	public static void imageWatermark(BufferedImage srcBufferedImage,File destFile,
		File watermarkFile,WatermarkPosition watermarkPosition, int alpha){
		
		try {
		int srcImageWidth = srcBufferedImage.getWidth();
		int srcImageHeigh = srcBufferedImage.getHeight();
		BufferedImage destBufferedImage = new BufferedImage(srcImageWidth, srcImageHeigh, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics2d = destBufferedImage.createGraphics();
		graphics2d.setBackground(Color.WHITE);
		graphics2d.clearRect(0, 0, srcImageWidth, srcImageHeigh);
		graphics2d.drawImage(srcBufferedImage.getScaledInstance(srcImageWidth, srcImageHeigh, Image.SCALE_SMOOTH), 0, 0, null);
		if (watermarkFile != null && watermarkFile.exists() && 
			watermarkPosition != null && watermarkPosition != WatermarkPosition.no) {
			
			BufferedImage watermarkBufferedImage = ImageIO.read(watermarkFile);
			int watermarkBufferedImageWidth = watermarkBufferedImage.getWidth();
			int watermarkBufferedImageHeigh = watermarkBufferedImage.getHeight();
			//目标色中的源色部分将被合成到目标色中（Porter-Duff Source Atop Destination 规则）,指定的规则和用来乘源色 alpha 值的常量 alpha 值
			graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha/100.0F));
			
			//水印的位置
			int x = 0;
			int y = 0;
			if (watermarkPosition == WatermarkPosition.topLeft) {
				x = 0;
				y = 0;
			} else if (watermarkPosition == WatermarkPosition.topRight) {
				x = srcImageWidth - watermarkBufferedImageWidth;
				y = 0;
			} else if (watermarkPosition == WatermarkPosition.center) {
				x = (srcImageWidth - watermarkBufferedImageWidth) / 2;
				y = (srcImageHeigh - watermarkBufferedImageHeigh) / 2;
			} else if (watermarkPosition == WatermarkPosition.bottomLeft) {
				x = 0;
				y = srcImageHeigh - watermarkBufferedImageHeigh;
			} else if (watermarkPosition == WatermarkPosition.bottomRight) {
				x = srcImageWidth - watermarkBufferedImageWidth;
				y = srcImageHeigh - watermarkBufferedImageHeigh;
			}
			graphics2d.drawImage(watermarkBufferedImage, x, y, watermarkBufferedImageWidth, watermarkBufferedImageHeigh, null);
		   
		 }
		    graphics2d.dispose();
		    ImageIO.write(destBufferedImage,"JPEG", destFile);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * 
	 * @param srcBufferedImage
	 * @param destFile
	 * @param destHeight
	 * @param destWidth
	 * @param watermarkFile
	 * @param watermarkPosition
	 * @param alpha
	 */
	public static void  zoomAndWatermark(BufferedImage srcBufferedImage,File destFile,int destHeight,int destWidth,File watermarkFile,WatermarkPosition watermarkPosition, int alpha){
		
	}

}
